//
//  DiscussCell.m
//  OperationPro
//
//  Created by Mac on 16/10/9.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "DiscussCell.h"
#import "DiscussModel.h"

@interface DiscussCell()
@property (nonatomic, strong) UILabel *lblName;
@property (nonatomic, strong) UIImageView *headImg;
@property (nonatomic, strong) UILabel *lblDate;
@property (nonatomic, strong) UILabel *lblType;
@property (nonatomic, strong) UILabel *lblCount;
@property (nonatomic, strong) UIImageView *chatView;
@end

@implementation DiscussCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)initView{
    [super initView];
    _headImg=[[UIImageView alloc]init];
    [self addSubview:_headImg];
    [_headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).mas_offset(20);
        make.top.equalTo(self).mas_offset(20);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    _headImg.layer.masksToBounds=YES;
    _headImg.layer.cornerRadius=25;
    _headImg.image=[UIImage imageNamed:@"Ico_home_head"];
    _lblName = [[UILabel alloc] init];
    [self addSubview:_lblName];
    [_lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImg.mas_right).mas_offset(10);
        make.top.equalTo(self.headImg);
    }];
    [_lblName setFont:F(12)];
    [_lblName setTextColor:LightFontColor];
    
    _lblDate = [[UILabel alloc] init];
    [self addSubview:_lblDate];
    [_lblDate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).mas_offset(-15);
        make.bottom.equalTo(_lblName);
    }];
    [_lblDate setTextColor:LightFontColor];
    [_lblDate setFont:F(12)];
    
    _lblType = [[UILabel alloc] init];
    [self addSubview:_lblType];
    [_lblType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lblName);
        make.bottom.equalTo(self).mas_offset(-15);
    }];
    [_lblType setTextColor:[UIColor whiteColor]];
    [_lblType setBackgroundColor:MainColor];
    [_lblType setFont:F(14)];
   
    _lblCount = [[UILabel alloc] init];
    [self addSubview:_lblCount];
    [_lblCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_lblDate);
        make.bottom.equalTo(_lblType);
    }];
    [_lblCount setTextColor:MainColor];
    [_lblCount setFont:_lblDate.font];
    
    UIImage *imgChat = [UIImage imageNamed:@"Ico_discuss_num"];
    UIImageView *chatView = [[UIImageView alloc] init];
    [self addSubview:chatView];
    [chatView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_lblCount);
        make.size.mas_equalTo(imgChat.size);
        make.right.equalTo(self.lblCount.mas_left).mas_offset(-3);
    }];
    [chatView setImage:imgChat];
    _chatView = chatView;
    
    [self addSubview:self.lblTitle];
    [self.lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).mas_offset(60);
        make.bottom.equalTo(_lblType);
        make.right.equalTo(chatView.mas_left).mas_offset(-3);
    }];
    [self.lblTitle setFont:F(14)];
    self.lblTitle.textAlignment=NSTextAlignmentLeft;
    [self.lblTitle setTextColor:FontColor];
    
    [self layoutDottedLineWithoriginY:80 WithAddSubview:self];
}

- (void)setModel:(DiscussModel *)model{
    NSString*urlString =[CClass getCacheData:qnDomain];

    if (model.D_headImg==nil||[model.D_headImg isEqualToString:@""]) {
        _headImg.image=[UIImage imageNamed:@"Ico_home_head"];

    }else
    {
        NSURL *headUrl =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,model.D_headImg]];
        [_headImg sd_setImageWithURL:headUrl placeholderImage:[UIImage imageNamed:@"Ico_home_head"]];

    }

    _lblName.text = model.D_author;
    NSString  *a = model.D_created;
    NSString *timeString =[a substringWithRange:NSMakeRange(0, 16)];
    _lblDate.text = timeString;
    NSDictionary *kind = @{@"01":@"[提问]", @"02":@"[讨论]", @"03":@"[经验]"};
    _lblType.text = F_NSpace(kind[model.D_kind]);
    self.lblTitle.text = [NSString stringWithFormat:@"%@%@", F_NSpace(kind[model.D_kind]), model.D_title];
//    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.lblTitle.text];
//    [attr addAttributes:@{NSForegroundColorAttributeName:C(@"7c7c7c")} range:NSMakeRange(0, [F_NSpace(kind[model.D_kind]) length])];
//    self.lblTitle.attributedText = attr;
    _lblCount.text = model.D_reply_count;
    BOOL isnice = [model.D_flag isEqualToString:@"01"];
    if (isnice) {
        _lblType.text = @" 精华 ";
    }else{
        _lblType.text = @"";
    }
    CGSize kindsize = [_lblType.text sizeWithFont:_lblType.font constrainedToSize:CGSizeMake(100, WindowHeight)];
    [self.lblTitle mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).mas_offset(80 + kindsize.width+5);
    }];
    
    CGSize size = [_lblCount.text sizeWithFont:_lblCount.font constrainedToSize:CGSizeMake(100, WindowHeight)];
    [_chatView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).mas_offset(-30 - size.width);
    }];
}

+ (float)getCellHeightByModel:(id)model{
    return 80;
}
- (void)layoutDottedLineWithoriginY:(float)originY WithAddSubview:(UIView *)view
{
    CGFloat width=10+35+14;
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    // 设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:C(@"d4d4d4").CGColor];
    // 3.0f设置虚线的宽度
    [shapeLayer setLineWidth:0.5f];
    [shapeLayer setLineJoin:kCALineJoinRound];
    // 3=线的宽度 1=每条线的间距
    [shapeLayer setLineDashPattern:
     [NSArray arrayWithObjects:[NSNumber numberWithInt:3],
      [NSNumber numberWithInt:1],nil]];
    // Setup the path
    CGMutablePathRef path = CGPathCreateMutable();
    // 0,10代表初始坐标的x，y
    // 320,10代表初始坐标的x，y
    CGPathMoveToPoint(path, NULL, width, originY-0.5);
    CGPathAddLineToPoint(path, NULL, WindowWidth,originY);
    [shapeLayer setPath:path];
    CGPathRelease(path);
    // 可以把self改成任何你想要的UIView, 下图演示就是放到UITableViewCell中的
    [[view layer] addSublayer:shapeLayer];
    
}
@end
